package lsl.leetCode.year2023.test0202;


import lsl.leetCode.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * description:二叉树的前序遍历:(根在前,从左往右,一棵树的根永远在左子树前面，左子树又永远在右子树前面)
 *
 * @author: liushengli
 * data: 2023/2/2
 */
public class test144 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        treeNode1.left = null;
        TreeNode treeNod2 = new TreeNode(2);
        treeNode1.right = treeNod2;
        TreeNode treeNod3 = new TreeNode(3);
        treeNod2.left = treeNod3;
        treeNod2.right = null;
        preOrderTraversal(treeNode1);
    }

    public static List<Integer> preOrderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        traversal(root, list);
        return list;
    }

    public static void traversal(TreeNode root, List<Integer> list) {
        list.add(root.val);
        if (root.left != null) {
            traversal(root.left, list);
        }
        if (root.right != null) {
            traversal(root.right, list);
        }
    }
}
